package com.yun.algorithmproblem.leetcode;

public class Leetcode541 {

    public static void main(String[] args) {
        Leetcode541 obj=new Leetcode541();
        String s="abcdefg";
        int k=2;
        obj.reverseStr(s,k);
    }
    public String reverseStr(String s, int k) {
        StringBuilder sbAns =new StringBuilder();
        StringBuilder sbReverse =new StringBuilder();
        while (s.length()>=k*2){
            String prefix = s.substring(0, k);
            s=s.substring(k);
            sbReverse.append(prefix);
            sbReverse.reverse();
            sbAns.append(sbReverse);
            sbReverse.setLength(0);
            String suffix = s.substring(0, k);
            s=s.substring(k);
            sbAns.append(suffix);
        }
        if(s.length()>=k){
            String prefix = s.substring(0, k);
            s=s.substring(k);
            sbReverse.append(prefix);
            sbReverse.reverse();
            sbAns.append(sbReverse);
            sbAns.append(s);
        }else {
            sbReverse.append(s);
            sbReverse.reverse();
            sbAns.append(sbReverse);
        }
        return sbAns.toString();
    }
}
